package text;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BallJPanel extends JPanel implements MouseListener, MouseMotionListener {
     public int hx;
     public int hy;
     public Image img_hero=new ImageIcon("3.gif").getImage();
     public  int[] xx;
     public  int[] yy;
     public Font[] fs;
     public List<Ball> foes;
     public List<Ball> fires;
     public  boolean isFire=false;

     public  BallJPanel(){
         hx=420;
         hy=900;

         xx=new int[300];
         yy=new int[300];
         fs=new Font[5];
         for (int i = 0; i <xx.length ; i++) {
             xx[i]=new Random().nextInt(800);
             yy[i]=new Random().nextInt(1000);
         }
         for (int i = 0; i <fs.length ; i++) {
             fs[i]=new Font("宋体",Font.BOLD,12+i*2);
         }
         foes =new ArrayList<Ball>();
         fires =new ArrayList<Ball>();

         this.addMouseListener(this);
         this.addMouseMotionListener(this);
     }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.setBackground(Color.BLACK);
        g.drawImage(img_hero,hx,hy,50,50,null);
        for (int i = 0; i <xx.length ; i++) {
            g.setColor(new Color(new Random().nextInt(0xffffff)));
            g.setFont(fs[new Random().nextInt(5)]);
            g.drawString(".",xx[i],yy[i]);
        }
        for (int i = 0; i <foes.size() ; i++) {
            Ball ball=foes.get(i);
            ball.draw(g);
        }
        for (int i = 0; i <fires.size() ; i++) {
            Ball ball=fires.get(i);
            ball.draw(g);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
isFire=true;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
isFire=false;
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        hx=e.getX()-20;
        hy=e.getY()-20;
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        hx=e.getX()-20;
        hy=e.getY()-20;
        repaint();
    }
    public void startRun(){
        Thread thread = new Thread(){
            @Override
            public void run() {
                int count=0;
                while (true){
                    count++;
                    if(count>=Integer.MAX_VALUE){
                        count=0;
                    }
                    try {
                        Thread.sleep(40);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(count%30==0){
                        Ball ball=new Ball();
                        ball.x=new Random().nextInt(800);
                        ball.y=-40;
                        ball.r=20;
                        ball.type=Ball.FOE;
                        ball.ori=Ball.DOWN;

                        foes.add(ball);
                    }
                    for (int i = 0; i <foes.size() ; i++) {
                        Ball ball=foes.get(i);
                        ball.move();
                        if(ball.y>=1000){
                            foes.remove(i);
                        }
                    }
                    if(isFire&&count%10==0){
                        Ball ball=new Ball();
                        ball.x=hx+5;
                        ball.y=hy-20;
                        ball.r=20;
                        ball.type=Ball.FIRE;
                        ball.ori=Ball.UP;
                        fires.add(ball);
                    }
                    for (int i = 0; i <fires.size() ; i++) {
                        Ball ball = fires.get(i);
                        ball.move();
                        if(ball.y<=0){
                            fires.remove(i);
                        }
                    }
                    for (int i = 0; i <fires.size() ; i++) {
                        for (int j = 0; j <foes.size() ; j++) {
                            if (i<fires.size()&&j<foes.size()&& fires.get(i).isTouch(foes.get(j))) {
                             fires.remove(i);
                             foes.remove(j);
                            }
                        }
                    }
                    repaint();
                }
            }
        };
        thread.start();

    }
}
